import "../css/stushow.css"
import $ from './jquery';
import './401';

$(function () {

    document.onkeydown = function (e) {

        var ev = document.all ? window.event : e;
        console.log(ev.keyCode);
        if (ev.keyCode == 13) {

            if ($("#search").is(":focus")) {
                myPageIndex = 1;
                search = $("#search").val().trim();
                list();
            }
        }
    }

    let myPageIndex = 1, myPageSize = 5, myPageCount, search = "";
    list()
    //学生列表
    function list() {
        $.ajax({
            url: 'http://192.168.30.63:3000/stus/getAll',
            type: "get",
            dataType: "json",
            data: {
                pagesize: myPageSize,
                pageindex: myPageIndex,
                search
            },
            beforeSend: function () {
                $("#load").show();
            },
            success: function (data) {
                if (data.code == 200) {

                    $("table tbody").empty();//清空之前数据

                    myPageCount = data.pagecount;
                    $(".page_this").html(myPageIndex);
                    $(".count").html(data.pagecount);
                    //如果总页数等于pageindex
                    $(".first,.prev,.next,.last").removeClass("disb");
                    if (myPageCount == myPageIndex || myPageCount == 0) {
                        $(".next").addClass("disb");
                        $(".last").addClass("disb");
                    }
                    //第一页
                    if (myPageIndex == 1) {
                        $(".first").addClass("disb");
                        $(".prev").addClass("disb");
                    }
                    data.result.forEach((item, index) => {
                        let head;
                        if (item.head == "" || item.head == undefined) {
                            head = "http://192.168.30.63:3000/images/head1.jpg";
                        } else {
                            head = "http://192.168.30.63:3000/images/" + item.head;
                        }

                        $("table tbody").append(`<tr>
                        <td><img src="${head}" ></td>
                            <td>${item.name}</td>
                            <td>${item.age}</td>
                            <td>${item.gender == 1 ? "男" : "女"}</td>     
                            <td>${item.clas_id.cname}</td>
                            <td>${item.birth}</td>
                            <td>${item.like}</td>             
                            <td><a class="upd" href="./update.html?id=${item._id}">修改</a><a class="del" data-id="${item._id}">删除</a></td>
                            </tr>`)
                    });
                }
            },
            complete: function () {
                $("#load").hide();
            }
        })
    }

    //每页显示
    $(".sel-psize").on("change", function () {
        myPageSize = $(this).val();
        myPageIndex = 1;
        list();
    });
    //首页
    $(".first").on("click", function () {
        if ($(this).hasClass("disb")) {
            return;
        }
        myPageIndex = 1;
        list();
    });
    //上一页
    $(".prev").on("click", function () {
        if ($(this).hasClass("disb")) {
            return;
        }
        myPageIndex--;
        list();
    });
    //下一页
    $(".next").on("click", function () {
        if ($(this).hasClass("disb")) {
            return;
        }
        myPageIndex++;
        list();
    });
    //尾页
    $(".last").on("click", function () {
        if ($(this).hasClass("disb")) {
            return;
        }
        myPageIndex = myPageCount;
        list();
    });

    //删除
    $("table").on("click", ".del", function () {
        if (confirm("确认删除吗")) {
            let ids = $(this).attr("data-id");
            console.log(ids);
            $.ajax({
                url: 'http://192.168.30.63:3000/stus/del',
                data: { id: ids },
                dataType: "json",
                type: "post",
                success: function (data) {
                    if (data.code == 200) {
                        // location.href = "./show.html";
                        console.log(data.code);
                        $(".del[data-id=" + ids + "]").parent().parent().remove();
                    } else {
                        alert("删除失败");
                    }
                }
            })
        }

    })

    //搜索
    $(".xg").click(function () {
        myPageIndex = 1;
        search = $("#search").val().trim();
        list();
    })
})
